package javabase.xml.jsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;

/**
 * @Auther: luzhanshi
 * @Date: 2021/2/21 - 02 - 21 - 22:42 
 * @Description: javabase.xml.jsoup
 * @version: 1.0
 */
public class JsoupDemo3 {
    public static void main(String[] args) throws IOException {
        String path = JsoupDemo3.class.getClassLoader().getResource("student.xml").getPath();
        Document document = Jsoup.parse(new File(path), "utf-8");
        //3.1获取所有student对象
        Elements elements = document.getElementsByTag("student");
        System.out.println(elements);
        System.out.println("-----------------------------------------");
        //3.2获取所有带有“number”属性的元素
        /**
         *     <student number="s1001">
         *         <name id="111">tom</name>
         *         <age>18</age>
         *         <sex>1</sex>
         *     </student>
         */
        Elements elementsByAttribute = document.getElementsByAttribute("number");
        System.out.println(elementsByAttribute);
        System.out.println("--------------------------------------------------------");
        //3.3获取所有带有“number”属性并且属性值为“s1001”的元素
        Elements elementsByAttributeValue = document.getElementsByAttributeValue("number", "s1001");
        System.out.println(elementsByAttributeValue);
        System.out.println("--------------------------------------------------------");
        //3.3获取“id”属性值为“s1001”的元素
        Element elementById = document.getElementById("111");
        System.out.println(elementById);

    }

}
